package com.woniu.product.controller;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class HttpController {

    @RequestMapping("/toBaidu")
    public String toBaidu() throws IOException {
        String url = "http://www.baidu.com";

        // 得到一个客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);

        // 发送一个get请求
        CloseableHttpResponse response = httpClient.execute(httpGet);

        // 得到内容
        HttpEntity entity = response.getEntity();

        String result = EntityUtils.toString(entity, "utf-8");

        return result;
    }
}
